package com.wolf.controller;

import com.wolf.pojo.Wl_category;
import com.wolf.service.Wl_categoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@ComponentScan
public class Wl_categoryController {

    @Autowired
    Wl_categoryService wl_categoryService;

    /**
     * 查询分类列表所有
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_category_get")
    public String getAll(ModelMap modelMap){
        //查询分类所有的信息
        List<Wl_category> list = wl_categoryService.getAll();

        //把list里的父类id拿出来查询得到分类名称   存入到map中
        Map<Wl_category,Object> parentName_Map =  FMap(list);
        
        modelMap.addAttribute("wl_category_parentName_Map",parentName_Map);
        return "table_Wl_category";
    }

    /**
     * 按名称查询
     * @param modelMap
     * @return
     */
    @RequestMapping("wl_category_getCateName")
    public String getCateName(ModelMap modelMap,String cateName,String parentName){
        //按名称查询父类
        if(parentName.equals("") && cateName.equals("")) {
            return "redirect:wl_category_get";
        }
        else if(parentName.equals("") || parentName==null){
            List<Wl_category> list = wl_categoryService.getCateName(cateName);

            Map<Wl_category,Object> parentName_Map =  FMap(list);

            modelMap.addAttribute("wl_category_parentName_Map",parentName_Map);
        }else if (cateName.equals("") || cateName==null){
            List<Wl_category> list = new ArrayList<Wl_category>();

            //按名称查询父类的id
            List<String> nameList = wl_categoryService.getCateId(parentName);

            //按父类的id查询子类
            for(String name : nameList){
                list.addAll(wl_categoryService.getChild(name));
            }

            Map<Wl_category,Object> parentName_Map =  FMap(list);

            modelMap.addAttribute("wl_category_parentName_Map",parentName_Map);
        }
        else {
            return "redirect:wl_category_get";
        }
        return "table_Wl_category";
    }

    /**
     * 封装查询Map方法
     * @param list
     * @return
     */
    private Map<Wl_category,Object> FMap(List<Wl_category> list) {
        Map<Wl_category,Object> parentName_Map = new HashMap<Wl_category, Object>();
        for (Wl_category a : list){
            if (a.getParentid()!=null){
                Object o = wl_categoryService.getByParentName(a.getParentid());
                parentName_Map.put(a,o);
            }else{
                parentName_Map.put(a,"--");
            }
        }
        return parentName_Map;
    }


    /**
     * 获得一个对象Info
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "getTypeInfo")
    public String getCatgoryInfo(String id, Model model)
    {
        Object val = wl_categoryService.selectByPrimaryKey(id);

        Object type = wl_categoryService.getAllSelective();

        model.addAttribute("category_list",type);

        model.addAttribute("typeInfo", wl_categoryService.selectByPrimaryKey(id));

        model.addAttribute("typeInfo", val);

        return "widgets";//widgets.html
    }
    @ModelAttribute
    public Wl_category setCategory()
    {
        return new Wl_category();
    }
    /**
     * 修改一个对象的Info
     * @param category
     * @param response
     * @return
     * @throws java.io.IOException
     */
    @ResponseBody
    @RequestMapping(value = "modifytype",method = RequestMethod.POST)
    public String modifyType( Wl_category category,HttpServletResponse response) throws java.io.IOException {
        int modifyRel = wl_categoryService.updateByPrimaryKey(category);
        return (modifyRel==1)+"";
    }

    /**
     * 删除
     */
    @RequestMapping("wl_category_remove")
    public String remove(String cateid){
        wl_categoryService.deleteByPrimaryKey(cateid);
        return "redirect:wl_category_get";
    }

    /**
     * 选择性新增
     */
    @RequestMapping("wl_category_insert")
    public String insert(Wl_category category){

        //判断传过来的父类编号是否为空，如果是则给Grade等级赋值为1
        //否则赋值为2
        if(category.getParentid().equals("1")){
            category.setGrade(1);

            //父类编号赋值为空
            category.setParentid(null);

        }else{
            category.setGrade(2);
        }

        //创建时间  默认为系统时间
        category.setCreatetime(new Date());

        int i = wl_categoryService.insertSelective(category);

        return "redirect:wl_category_get";
    }

    /**
     * 查询所有，只查询所需要的列
     */
    @RequestMapping("wl_category_getSelective")
    public String getSelective(ModelMap modelMap){

        List<Wl_category> list = wl_categoryService.getAllSelective();

        modelMap.addAttribute("category_list",list);

        return "Wl_Category_Insert";
    }

    /**
     * 查询名称
     * 给商品新增里的下拉框
     */
    @RequestMapping("wl_category_getAllCateName")
    public String getAllCateName(ModelMap modelMap){

        List<Wl_category> list = wl_categoryService.getAllCateName();

        modelMap.addAttribute("wl_category_getAllCateName_List",list);

        return "wl_commodity_Insert";
    }

    /**
     * 查询名称
     * 给商品修改里的下拉框
     */
    @RequestMapping("wl_category_getAllUpdate")
    public String wl_category_getAllUpdate(ModelMap modelMap){

        List<Wl_category> list = wl_categoryService.getAllCateName();

        modelMap.addAttribute("wl_category_getAllCateName_List",list);

        return "wl_commodity_Update";
    }

    /**
     * 查询名称
     * 给活动管理新增的第一级下拉框
     */
    @RequestMapping("wl_category_getByCateId")
    public String wl_category_getByCateId(ModelMap modelMap){

        List<Wl_category> list = wl_categoryService.getByCateId();

        modelMap.addAttribute("wl_category_getByCateId_List",list);

        return "wl_activity_commodity_Insert";
    }

    /**
     * 优惠卷管理页面查询所有商品分类
     */
    @RequestMapping("wl_coupon_getByCateName")
    public String Wl_coupon_getByCateName(ModelMap modelMap){

        List<Wl_category> list = wl_categoryService.Wl_coupon_getByCateName();

        modelMap.addAttribute("Wl_coupon_getByCateName",list);

        return "table_Wl_coupon";
    }

    /**
     * 优惠卷管理新增页面商品分类
     */
    @RequestMapping("wl_coupon_InByCateName")
    public String wl_coupon_InsertByCateName(ModelMap modelMap){

        List<Wl_category> list = wl_categoryService.Wl_coupon_getByCateName();

        modelMap.addAttribute("wl_coupon_InsertByCateName",list);

        return "wl_coupon_Insert";
    }
}
